<?php

namespace App\Console\Commands;

use App\Imports\HospitalImport;
use App\Jobs\SyncDataChild;
use App\Jobs\TempPush;
use App\Models\FamilyEdu;
use App\Models\Hospital;
use App\Console\Commands\HospitalAppointInterval;
use App\Models\HospitalAppointIntervalNum;
use App\Models\FamilyType;
use App\Models\HospitalImportChild;
use App\Models\HospitalUploadData;
use App\Models\old\HospitalAppoint;
use App\Models\old\HospitalAppointStreet;
use App\Models\old\HospitalAppointVaccineTimeNum;
use App\Models\old\HospitalAppointWeek;
use App\Models\old\OldHospital;
use App\Models\old\OldHospitals;
use App\Models\old\Street;
use App\Models\TempPushRecord;
use App\Models\Vaccine;
use App\Wechat\Init;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use App\Exceptions\Arr;

class Test extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {


        $ak= env('OSS_ACCESS_KEY_ID');

        $sk = getenv('OSS_ACCESS_KEY_SECRET');
        $token =  getenv('OSS_SESSION_TOKEN');


        var_dump($ak);exit;
        $wechat = new Init();
        $app = $wechat->getApp();
        $ticket = $app->getTicket();
        $signPackage = $ticket->configSignature('12','12','12');

        var_dump($signPackage);exit;
        Vaccine::query()
            ->whereIn('id',['45','57','58','59','97','144','145'])
            ->update(['intro'=>"接种时请您携带身份证及儿宝宝预约二维码，幼儿园及学校学生接种须携带接种本。为防止倒号行为，现场将实名扫码核销二维码。二维码中姓名与实际姓名不同者或者预约时间不是当天者不提供接种服务。"]);
        exit;


        $sql = "SHOW PROCESSLIST;";
        $list = DB::select($sql);
        foreach($list as $v){
            if($v->Command=='Sleep'){
                $sql = "kill ".$v->Id;
                var_dump($sql);

                DB::statement($sql);
            }
        }
        //var_dump($list);exit;
        exit;


        $app = app('easywechat.mini_app');
        $api = $app->getClient();
        $response = $api->postJson('/wxa/queryscheme',[
            'scheme'=>'weixin://dl/business/?appid=wx1147c2e491dfdf1d&path=pages/appoint/list'
        ]);
        $wxResponse = json_decode($response->getContent(),true); // 这里会抛出异常

        var_dump($wxResponse);exit;



        $appointsArr = [
            [
                'id'=>1,
                'appoint_time'=>1
            ],
            [
                'id'=>2,
                'appoint_time'=>1
            ],
            [
                'id'=>3,
                'appoint_time'=>1
            ],
        ];
        $intervalNum = array_filter($appointsArr, function ($where)  {
            return $where['id'] == 1;
        });

        var_dump($intervalNum);exit;

        $hospitalUploadData = HospitalUploadData::query()->where('id',64)->first();
        $hospitalid = $hospitalUploadData->hospital_id;
        $provider = new EnvironmentVariableCredentialsProvider();
        $bucket= config('filesystems.disks.hospitalData.bucket');
        $object = $hospitalid.'/'.$hospitalUploadData->file_name;
        $localfile = "/tmp/" . $hospitalid . ".xlsx";
        $config = array(
            "provider" => $provider,
            "endpoint" => 'oss-cn-beijing.aliyuncs.com',
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-beijing"
        );
        $ossClient = new OssClient($config);
        $options = array(
            OssClient::OSS_FILE_DOWNLOAD => $localfile
        );
        $ossClient->getObject($bucket,$object, $options);


        if (empty($localfile) OR !file_exists($localfile)) {
            $this->fail('临时文件不存在');
        }
        Excel::import(new HospitalImport($hospitalUploadData), $localfile);
        exit;



        $model = HospitalImportChild::query()->where('id',82)->first();
        var_dump($model);exit;
        SyncDataChild::dispatch($model);
        exit;

        $models = FamilyEdu::query()
            ->select(DB::raw('ANY_VALUE(id) as id,ANY_VALUE(family_id) as family_id,ANY_VALUE(article_id) as article_id,ANY_VALUE(family_type_id) as family_type_id,user_id'))
            ->whereIn('family_id', [39,41])
            ->groupBy('user_id')
            ->get();
        if ($models->isNotEmpty()) {
            //TempPushRecord::upsert($data,['user_id','temp_id']);

            foreach ($models as $model) {
                $tempPushRecord = TempPushRecord::createTemp($model->user_id, 	5);
                TempPush::dispatch($tempPushRecord, $model);
            }
        }else{
            $this->fail('数据为空id全部不存在');
        }
        return 0;
    }
}
